package controler;
import  model.Bibliography;
import model.beans.BibliographyBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Academy;
import model.Subject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;

import business.TipoBibliografia;
import business.misc.Util;

public class BibliographyAction extends MappingDispatchAction {
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));    	
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	//request.setAttribute("lst_bibliografia", Bibliography.list(str_academyCode, str_subjectCode));
    	
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		
		request.setAttribute("tipos", TipoBibliografia.lista(this.getResources(request)));
		
		DynaActionForm forma = (DynaActionForm)form;
		BibliographyBean biblio = Bibliography.instance( ((Integer)forma.get("id")).intValue() );
		biblio.setAcademy(str_academyCode);
		biblio.setSubject(str_subjectCode);
		
		forma.set("titulo", biblio.getTitle());
		forma.set("autor", biblio.getAuthor());
		forma.set("editorial", biblio.getEditorial());
		forma.set("anio", new Integer(biblio.getYear()));
		forma.set("issn", biblio.getIssn());
		forma.set("isbn", biblio.getIsbn());
		forma.set("tipo", biblio.getType());
		
		return mapping.findForward(Util.DEFAULT);
	}


public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	DynaActionForm forma = (DynaActionForm)form;
	
	String str_academyCode = request.getParameter("academy_id");
	String str_subjectCode = request.getParameter("subject_id");
	request.setAttribute("academia", Academy.instance(str_academyCode));
	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
	
	String str_forward;
	if( ((Integer)forma.get("id")).intValue() == 0 ){
		if( model.Bibliography.add(forma.getMap(), str_subjectCode) > 0 ){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
			str_forward = Util.SUCCESS;
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
			str_forward = Util.FAIL+Util.ADD;
		}
	}else{
		if( model.Bibliography.edit(forma.getMap()) > 0 ){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
			str_forward = Util.SUCCESS;
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
			str_forward = Util.FAIL+Util.EDIT;
		}
	}
	
	if(str_forward.equals(Util.SUCCESS)){
		HttpSession session = request.getSession(); 
		session.setAttribute("inst_bibliografia", model.Bibliography.list(str_academyCode,str_subjectCode));
	}
	
	//request.setAttribute("inst_bibliografia", Bibliography.list(str_academyCode, str_subjectCode));
	return mapping.findForward(str_forward);
}

public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
	if(action_retVal!=null)return action_retVal;
	
	DynaActionForm forma = (DynaActionForm)form;
	forma.initialize(mapping);
	forma.reset(mapping, request);
	
	String str_academyCode = request.getParameter("academy_id");
	String str_subjectCode = request.getParameter("subject_id");
	request.setAttribute("academia", Academy.instance(str_academyCode));
	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
	request.setAttribute("tipos", TipoBibliografia.lista(this.getResources(request)));
	
	return mapping.findForward(Util.DEFAULT);
}

public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
	if(action_retVal!=null)return action_retVal;
	
	String str_academyCode = request.getParameter("academy_id");
	String str_subjectCode = request.getParameter("subject_id");
	
	request.setAttribute("academia", Academy.instance(str_academyCode));
	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
	
	DynaActionForm forma = (DynaActionForm)form;
	if( Bibliography.delete(((Integer)forma.get("id")).intValue()) > 0 ){
		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessDelete"));
	}else{
		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailDelete"));
	}
	HttpSession session = request.getSession(); 
	session.setAttribute("inst_bibliografia", model.Bibliography.list(str_academyCode,str_subjectCode));
	return mapping.findForward(Util.DEFAULT);
}
}
